__author__ = 'tyrion'


from src.CreateItemPlugins.Base import BaseCreateMonitors
from src.common.tools.salt import *
from conf.gloal import *


CHECK_MK_MRPE_PATH = "/etc/check_mk/"
SALT_PLUGIN_PATH = "/opt/infrastructure/plugin/salt/master"
MRPE = "mrpe.cfg"


class SaltMonitors(BaseCreateMonitors):

    def __init__(self, conf):
        self.ip = conf.get("ip")
        self.hostName = conf.get("hostname")
        return

    def CreateMonitors(self):

        getPluginCmd = "wget http://%s/%s/%s" %(FILE_SERVER, SALT_PLUGIN_PATH, MRPE)
        getRet = ExecuteCommand(self.ip, getPluginCmd)

        getStatus = AnalyseResult(getRet, "wget plugin from file server failed", self.ip)
        if getStatus == "false":
            return

        ExecuteCommand(self.ip, ("mkdir  /etc/check_mk/"))

        mvRet = ExecuteCommand(self.ip, ("mv /%s  %s"% ( MRPE, CHECK_MK_MRPE_PATH)))
        mvErrorMsg = "mv plugin failed"
        mvStatus = AnalyseResult(mvRet, mvErrorMsg, self.ip)
        if mvStatus == "false":
            return



        return

    def AddToMonitorSvr(self):
        print("info: add tomcat monitors to monitor svr")
        cmdI = "cmk -I %s " %(self.hostName)
        cmdIRet = ExecuteCommand(ICINGA_SERVER,  cmdI)
        cmdIErrMsg = "cmk -I failed"
        cmdIStatus = AnalyseResult(cmdIRet, cmdIErrMsg, ICINGA_SERVER)
        if cmdIStatus == "false":
            return

        cmdO = "cmk -O"
        cmdORet = ExecuteCommand(ICINGA_SERVER, cmdO)
        cmdOErrMsg = "cmk - O failed"
        cmdOStatus = AnalyseResult(cmdORet, cmdOErrMsg, ICINGA_SERVER)
        if cmdOStatus == "false":
            return


        return



if __name__ == "__main__":

    print("info: this is main ")

    conf = {"hostname": "flexweb-cl-1", "ip":"172.24.133.22"}
    # servers = [{"hostname": "flexweb-cl-2", "ip":"172.24.133.28"},\
    #     {"hostname": "flexweb-cl-3", "ip":"172.24.133.76"},\
    #     {"hostname": "flexweb-cl-4", "ip":"172.24.133.86"},\
    #     {"hostname": "flexweb-cl-5", "ip":"172.24.133.87"}, \
    #     {"hostname": "flexweb-cl-6", "ip":"172.24.133.9"}]

    # servers = [{"hostname": "flexweb-cl-7", "ip":"172.24.133.116"},\
    #     {"hostname": "flexweb-cl-8", "ip":"172.24.133.123"},\
    #     {"hostname": "flexweb-cl-9", "ip":"172.24.133.39"},\
    #     {"hostname": "flexweb-cl-10", "ip":"172.24.133.40"},\
    #     {"hostname": "flexweb-cl-11", "ip":"172.24.133.41"}, \
    #     {"hostname": "flexweb-cl-12", "ip":"172.24.133.42"}, \
    #     {"hostname": "flexweb-cl-13", "ip":"172.24.133.43"}, \
    #     {"hostname": "flexweb-cl-14", "ip":"172.24.133.44"}, \
    #     {"hostname": "flexweb-cl-15", "ip":"172.24.133.45"}, \
    #     {"hostname": "flexweb-cl-16", "ip":"172.24.133.146"},\
    #     {"hostname": "flexweb-cl-17", "ip":"172.24.133.147"},\
    #     {"hostname": "flexweb-cl-18", "ip":"172.24.133.148"},\
    #     {"hostname": "flexweb-cl-19", "ip":"172.24.133.68"},\
    #     {"hostname": "flexweb-cl-20", "ip":"172.24.133.77"},\
    #     {"hostname": "flexweb-cl-21", "ip":"172.24.133.78"},\
    #     {"hostname": "flexweb-cl-22", "ip":"172.24.133.79"}]
    # servers = [{"hostname": "flexweb-wx-1", "ip":"221.228.81.134"},\
    #     {"hostname": "flexweb-wx-2", "ip":"221.228.81.135"},\
    #     {"hostname": "flexweb-wx-3", "ip":"221.228.81.137"},\
    #     {"hostname": "flexweb-wx-4", "ip":"221.228.81.138"},\
    #     {"hostname": "flexweb-wx-5", "ip":"221.228.81.157"},\
    #     {"hostname": "flexweb-wx-6", "ip":"221.228.81.136"}, \
    #     {"hostname": "flexweb-wx-7", "ip":"221.228.81.217"},\
    #     {"hostname": "flexweb-wx-8", "ip":"221.228.81.133"},\
    #     {"hostname": "flexweb-wx-9", "ip":"221.228.81.162"},\
    #     {"hostname": "flexweb-wx-10", "ip":"221.228.81.218"}]

    # servers = [{"hostname": "flexweb-wx-8", "ip":"221.228.81.133"}]
    # servers= [{"hostname": "flexweb-cl-9", "ip":"172.24.133.39"}]
    servers= [{"hostname": "flexweb-cl-6", "ip":"172.24.133.9"}]

    for server in servers:

        saltM = SaltMonitors(server)
        # saltM.CreateMonitors()
        saltM.AddToMonitorSvr()

    # ExecuteCommand("221.228.81.133", ("cat /etc/check_mk/mrpe.cfg"))